package org.jmc.refmanager.domain.dao;

import java.io.Serializable;
import java.util.Arrays;

/**
 * Encapsulates the Query Parameters to be passed to DAO or Service
 *
 */
public class QueryParam<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	private int first;
	private int count;
	private String sortPropertyName;
	private boolean sortAsc;
	private String[] excludeProperties;
	private T criteriaExample;

	/**
	 * 
	 */
	public QueryParam() {
		this(0, 0, null, true);
	}
	
	/**
	 * Set to return <tt>count</tt> elements, starting at the <tt>first</tt>
	 * element.
	 *
	 * @param first
	 *            First element to return (0-based).
	 * @param count
	 *            Number of elements to return.
	 */
	public QueryParam(int first, int count) {
		this(first, count, null, true);
	}

	/**
	 * Set to return <tt>count</tt> sorted elements, starting at the
	 * <tt>first</tt> element.
	 *
	 * @param first
	 *            First element to return.
	 * @param count
	 *            Number of elements to return.
	 * @param sortPropertyName
	 *            Column to sort on.
	 * @param sortAsc
	 *            Sort ascending or descending.
	 */
	public QueryParam(int first, int count, String sortPropertyName, boolean sortAsc) {
		this.first = first;
		this.count = count;
		this.sortPropertyName = sortPropertyName;
		this.sortAsc = sortAsc;
	}

	public void setSortPropertyName(String sortPropertyName) {
		this.sortPropertyName = sortPropertyName;
	}

	public void setSortAsc(boolean sortAsc) {
		this.sortAsc = sortAsc;
	}

	public int getCount() {
		return count;
	}

	public int getFirst() {
		return first;
	}

	public String getSortPropertyName() {
		return sortPropertyName;
	}

	public boolean isSortAsc() {
		return sortAsc;
	}

	public boolean hasSort() {
		return sortPropertyName != null;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	public void setCount(int count) {
		this.count = count;
	}

	/**
	 * @return the excludeProperties
	 */
	public String[] getExcludeProperties() {
		return excludeProperties;
	}

	/**
	 * @param excludeProperties the excludeProperties to set
	 */
	public void setExcludeProperties(String[] excludeProperties) {
		this.excludeProperties = excludeProperties;
	}

	/**
	 * @return the entityExample
	 */
	public T getCriteriaExample() {
		return criteriaExample;
	}

	/**
	 * @param entityExample the entityExample to set
	 */
	public void setCriteriaExample(T criteriaExample) {
		this.criteriaExample = criteriaExample;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return String
				.format("QueryParam [first=%s, count=%s, sortPropertyName=%s, sortAsc=%s, excludeProperties=%s, criteriaExample=%s]",
						first, count, sortPropertyName, sortAsc,
						Arrays.toString(excludeProperties), criteriaExample);
	}
	

}
